<template>
  <div class="header">
      <div class="left">
          <span class="iconfont iconjiantou2" @click="$router.back()"></span>
          <span class="iconfont iconnew"></span>
      </div>
      <div class="btnFollowActive" v-if="!post.has_follow" @click="follow">
          关注
      </div>
      <div class="btnFollow" v-else-if="post.has_follow" @click="unfollow">已关注</div>
  </div>
</template>

<script>
export default {
    props:[
        'post'
    ],
    methods:{
        follow(){
            // 发送ajax，请求关注用户
            this.$axios({
                url:'/user_follows/' +this.post.user.id,
                method:'get',
                headers:{
                 Authorization: localStorage.getItem('token')
                }
            }).then(res=>{
                const {message} = res.data;
                console.log(message);
                // console.log(res.data);
                if(message == '关注成功'){
                    this.post.has_follow = true;
                }
            })
        },
        unfollow(){
            this.$axios({
                url: '/user_unfollow/'+this.post.user.id,
                method: 'get',
                headers:{
                 Authorization: localStorage.getItem('token')
                }
            }).then(res=>{
                const {message} =res.data;
                console.log(message);
                // 如果我们点击了已关注,返回的 message == '取消关注成功'
                // 那么这个请求就算完成了

                if (message == "取消关注成功") {
                    this.post.has_follow = false;
                }                
            })
        }
    }
}
</script>

<style lang="less" scoped>
.header{
    display: flex;
    height: 13.889vw;
    justify-content: space-between;
    align-items: center;
    padding: 0 2.778vw;
}
.left{
    display: flex;
    align-items: center;
}
.iconnew{
    margin-left: 1.111vw;
    font-size: 15vw;
}
.btnFollowActive{
    background: red;
    color: white;
    height: 7.222vw;
    line-height: 7.222vw;
    padding: 0 4.167vw;
    border-radius: 3.611vw;
    border: 1px solid red;
}
.btnFollow{
    color: #888;
    padding: 0 4.167vw;
    height: 7.222vw;
    line-height: 7.222vw;
    border-radius: 3.611vw;
    border: 1px solid #888;
}
</style>